<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends BaseController {

    public function index() {
        if($_GET['cate']){
            $cate_id = $_GET['cate'];
            $article = D('Article');
            $count = M('article')->where(array('a_id' => $cate_id))->count();
            $p = getpage($count, 6);
            $show = $p -> show();
            $list = $article->page(I('get.p'))->join('web_article_cate ON web_article_cate.cate_id = web_article.cate_id')->where(array('web_article_cate.cate_id' => $cate_id))-> order('a_id desc')-> limit(6)->select();
            $this -> assign('list', $list);
            $this -> assign('page', $show);
        }else{
            $article = D('Article');
            $count = M('article')->count();
            $p = getpage($count, 6);
            $show = $p -> show();
            $list = $article->page(I('get.p'))->join('web_article_cate ON web_article_cate.cate_id = web_article.cate_id') -> order('a_id desc')-> limit(6)->select();
            $this -> assign('list', $list);
            $this -> assign('page', $show);
        }
        //分类菜单
        $article_cate = D('article_cate');
        $cate = $article_cate -> select();
        $this -> assign('cate_list', $cate);

        $this->display();
    }
    //搜索
    public function  search(){
        $like = I('keywords');
        $map['a_title'] = array('like',"%{$like}%");
        $article = D('Article')->where($map);
        $article_cate = D('article_cate');
        $cate = $article_cate ->where($map)-> select();
        $count = M('article')->where($map)->count();
        $p = getpage($count, 6);
        $show = $p -> show();
        $list = $article->page(I('get.p'))->join('web_article_cate ON web_article_cate.cate_id = web_article.cate_id') -> order('a_id desc')-> limit(6)->select();
        $this -> assign('list', $list);
        $this -> assign('page', $show);

        $this->display('index');
    }

}
